﻿using System;
using System.Globalization;
using System.Threading;
using System.Web;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;

namespace MusicLibrary
{
	public class MvcApplication : System.Web.HttpApplication
	{
		protected void Application_Start ()
		{
			AreaRegistration.RegisterAllAreas();

			FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
			RouteConfig.RegisterRoutes(RouteTable.Routes);
			BundleConfig.RegisterBundles(BundleTable.Bundles);
		}

		//protected void Application_BeginRequest(object sender, EventArgs e)
		protected void Application_AcquireRequestState(object sender, EventArgs e) // because Session is used
		{
			if (HttpContext.Current.Session != null)
			{
				//CultureInfo ci = new CultureInfo("de");
				CultureInfo ci = (CultureInfo) this.Session["Culture"];
				if (ci == null)
				{
					string langName = "en";
					if (HttpContext.Current.Request.UserLanguages != null
						&& HttpContext.Current.Request.UserLanguages.Length != 0)
					{
						langName = HttpContext.Current.Request.UserLanguages[0].Substring(0, 2);
					}
					ci = new CultureInfo(langName);
					this.Session["culture"] = ci;
				}
				Thread.CurrentThread.CurrentUICulture = ci;
				Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(ci.Name);
			}
		}
	}
}